package com.dfbz.bigdata.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dfbz.bigdata.common.utils.R;
import com.dfbz.bigdata.modules.system.doman.UserDesignerandpersonal;
import com.dfbz.bigdata.modules.system.service.UserDesignerandpersonalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Auther:东方标准
 * @Date:2022/9/25-09-25-20:03
 * @Decsription:com.dfbz.bigdata.modules.system.controller
 * @Version:1.0
 */
@RestController
@RequestMapping("/uduser")
@Slf4j
public class UserDesignerandpersonalController {
    @Autowired
    private UserDesignerandpersonalService userDesignerandpersonalService;

    @GetMapping
    public R<Page> getuser(String username,Long currentPage,Long pagesizes){

        log.info("页码{},每页数量{}",currentPage,pagesizes);
        System.out.println("UserDesignerandpersonalController.getuser");

        Page page =new Page(currentPage,pagesizes);

        LambdaQueryWrapper<UserDesignerandpersonal> lqw=new LambdaQueryWrapper<>();
        lqw.like(!username.isEmpty(),UserDesignerandpersonal::getUserName,username);
//        List<UserDesignerandpersonal> userDesignerandpersonal = userDesignerandpersonalService.list(lqw);
        userDesignerandpersonalService.page(page, lqw);

        return R.success(page);
    }


    @GetMapping("/auth/{userId}")
    public R<UserDesignerandpersonal> getById(@PathVariable String userId){
        UserDesignerandpersonal user = userDesignerandpersonalService.getById(userId);
        return R.success(user);
    }


    @GetMapping("/{id}")
    public R<UserDesignerandpersonal> getByIdUserandub(@PathVariable String id){
        System.out.println("UserDesignerandpersonalController.getByIdUserandub");
        LambdaQueryWrapper<UserDesignerandpersonal> lqw=new LambdaQueryWrapper<>();
        lqw.eq(UserDesignerandpersonal::getOpenId,id);
        UserDesignerandpersonal userDesignerandpersonal = userDesignerandpersonalService.getOne(lqw);
        System.out.println();
        return R.success(userDesignerandpersonal);
    }

    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable Long id){
        boolean b = userDesignerandpersonalService.removeById(id);
        if (b){
            return R.success("删除成功");
        }
        else return  R.error("系统繁忙");
    }
    @PutMapping("/put/{id}/{mark}/{count}")
    public R<String> updata(@PathVariable Long id,@PathVariable Integer mark,@PathVariable Integer count){
        UpdateWrapper<UserDesignerandpersonal> uw=new UpdateWrapper();
        uw.eq("user_id",id);
        UserDesignerandpersonal user=new UserDesignerandpersonal();
        user.setUserMark(mark);
        user.setUserCountTable(count);
        boolean update = userDesignerandpersonalService.update(user, uw);

        if (update){
            return R.success("修改成功");
        }
        return R.success("系统繁忙");
    }
    @PutMapping
    public R<String> update(@RequestBody UserDesignerandpersonal user){
        log.info("{}-----user",user);
        LambdaQueryWrapper<UserDesignerandpersonal> lqw=new LambdaQueryWrapper<>();
        lqw.eq(UserDesignerandpersonal::getOpenId,user.getOpenId());
        boolean b = userDesignerandpersonalService.update(user,lqw);
        if (b){
            return R.success("修改成功");
        }
        else return  R.error("系统繁忙");

    }

    @PutMapping("auth")
    public R<String> updateAuth(@RequestBody UserDesignerandpersonal userAuth){
        log.info("{}-----user",userAuth);
        userAuth.setUpdateTime(new Date());
        userAuth.setUserLabel("初级设计师");
        UpdateWrapper<UserDesignerandpersonal> lqw=new UpdateWrapper<>();
        lqw.lambda().eq(UserDesignerandpersonal::getUserId,userAuth.getUserId());
        lqw.lambda().set(UserDesignerandpersonal ::getUserDesignerStyle,userAuth.getUserDesignerStyle());
        lqw.lambda().set(UserDesignerandpersonal ::getUserText,userAuth.getUserText());
        lqw.lambda().set(UserDesignerandpersonal ::getUserrCertify,userAuth.getUserrCertify());
        lqw.lambda().set(UserDesignerandpersonal ::getUpdateTime,userAuth.getUpdateTime());
        lqw.lambda().set(UserDesignerandpersonal ::getUserLabel,userAuth.getUserLabel());

        boolean b = userDesignerandpersonalService.update(lqw);

        if (b){
            return R.success("认证成功");
        }
        else return  R.error("系统繁忙");

//        return R.success("test");

    }
}
